#include "clientinfodialog.h"
#include "ui_clientinfodialog.h"
#include "clientsdatabase.h"
#include "utils.h"


ClientInfoDialog::ClientInfoDialog(IpConnection *ic, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ClientInfoDialog)
{
    ui->setupUi(this);
    this->ic = ic;
    if(!ic) close();

    connect(ic, SIGNAL(destroyed()), this, SLOT(clientHasDestroyed()));

    this->setWindowTitle(ic->_ip);

    connect(&timer, SIGNAL(timeout()), this, SLOT(updateInfo()));
    timer.start(1000);
    updateInfo();
}



ClientInfoDialog::~ClientInfoDialog()
{
    qWarning() << "ClientInfoDialog destroyed";
    timer.stop();
    delete ui;
}


void ClientInfoDialog::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event)
    deleteLater();
}


void ClientInfoDialog::clientHasDestroyed()
{
    close();
}


void ClientInfoDialog::updateInfo()
{
    QString range;
    double r_size = Utils::speedToHigherDimensions(ic->threadsUploadSpeed(), range);
    ui->up_speed_label->setText( QString("%1 %2").arg(QString::number(r_size, 'f', 2)).arg(range) );

    r_size = Utils::bytesToHigherDimensions(ic->threadsTotalDownload(), range);
    ui->down_size_label->setText( QString("%1 %2").arg(QString::number(r_size, 'f', 2)).arg(range) );

    r_size = Utils::bytesToHigherDimensions(ic->threadsTotalUpload(), range);
    ui->up_size_label->setText( QString("%1 %2").arg(QString::number(r_size, 'f', 2)).arg(range) );

    ui->threads_label->setText( QString::number(ic->size()) );

    ui->files_listWidget->clear();
    QStringList list = ic->threadsObjectList();
    ui->files_listWidget->addItems(list);
}


void ClientInfoDialog::on_pushButton_clicked()
{
    close();
}


void ClientInfoDialog::on_pushButton_2_clicked()
{
    ic->abort();
}
